package com.example.as_sum.activity;

import android.animation.LayoutTransition;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.example.as_sum.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AnimViewGroupActivity extends AppCompatActivity {
    @Bind(R.id.anim_view_contain)
    LinearLayout Layout;
    @Bind(R.id.anim_view_layout)
    LinearLayout animViewLayout;
    @Bind(R.id.anim_view_contain2)
    LinearLayout Layout2;
    @Bind(R.id.anim_view_layout2)
    LinearLayout animViewLayout2;
    private LayoutTransition layoutTransition;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anim_view_group);
        ButterKnife.bind(this);
        int []location=new int[2];
        Log.e("===当前位置===>","x:"+location[0]+",y:"+location[1]);
        animViewLayout.getLocationInWindow(location);
        //开始的动画
        ValueAnimator animatorAppEaring=ValueAnimator.ofInt(0,500);
        animatorAppEaring.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int x=(int)animation.getAnimatedValue();
                animViewLayout.setX(x);
            }
        });
        //消失的动画
        ValueAnimator animatorDisAppEaring =ValueAnimator.ofFloat(0,1);
        animatorDisAppEaring.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float x=(float)animation.getAnimatedValue();
                animViewLayout.setAlpha(1-x);
            }
        });
        layoutTransition=new LayoutTransition();
        layoutTransition.setAnimateParentHierarchy(true);//默认是true
        //view消失动画，动画时间
        layoutTransition.setDuration(LayoutTransition.DISAPPEARING,2000);
        layoutTransition.setAnimator(LayoutTransition.DISAPPEARING,animatorDisAppEaring);
        //view出现动画，动画时间
        layoutTransition.setDuration(LayoutTransition.APPEARING,4000);
        layoutTransition.setAnimator(LayoutTransition.APPEARING,animatorAppEaring);
//        layoutTransition.setInterpolator(LayoutTransition.DISAPPEARING,new LinearInterpolator());
        //view 动画改变时，布局中的每个子view动画的时间间隔2秒
//        layoutTransition.setStagger(LayoutTransition.DISAPPEARING,2000);
        //view 动画延迟2秒开始
//        layoutTransition.setStartDelay(LayoutTransition.DISAPPEARING,2000);

        Layout.setLayoutTransition(layoutTransition);
    }

    @OnClick({R.id.anim_view_btn1, R.id.anim_view_btn2, R.id.anim_view_btn3, R.id.anim_view_btn4, R.id.anim_view_btn5})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.anim_view_btn1:
                animViewLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.anim_view_btn2:
                animViewLayout.setVisibility(View.INVISIBLE);
                break;
            case R.id.anim_view_btn3:
                animViewLayout2.setVisibility(View.VISIBLE);
                break;
            case R.id.anim_view_btn4:
                Animation animation=AnimationUtils.loadAnimation(this,R.anim.view_show);
                animViewLayout2.startAnimation(animation);
                break;
            case R.id.anim_view_btn5:
                Animation animation2=AnimationUtils.loadAnimation(this,R.anim.view_show2);
                animViewLayout2.startAnimation(animation2);
                break;
        }
    }
}
